﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http;
using System.Globalization;
using System.Web.Security;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.Models;
using PNProject.EF;
using PNProject;
using System.Net;

namespace PNProject.Controllers
{
    public class PublicAPIController : ApiController
    {
        //
        // GET: /PublicAPI/
        [HttpGet]
        public HttpResponseMessage RegisterEmail(string email, string l)
        {
            email = email.ToLower();
            if (l == "vi-VN")//Viet Nam
            {
                if (string.IsNullOrEmpty(email))
                {
                    return new HttpResponseMessage()
                    {
                        Content = new StringContent("Email không hợp lệ")
                    };
                }
                if (PNLibraries.isValidEmail(email))
                {
                    var dbo = StaticData.emailRepo.GetOne(a => a.Email == email);
                    if (dbo != null)
                    {
                        return new HttpResponseMessage()
                        {
                            Content = new StringContent("Email đã được đăng ký")
                        };
                    }
                    else
                    {
                        TBEmail mail = new TBEmail();
                        mail.Email = email;
                        mail.Type = true;
                        mail.Enable = true;
                        StaticData.emailRepo.Save(null, mail);
                        return new HttpResponseMessage()
                        {
                            Content = new StringContent("Bạn đã đăng ký thành công!")
                        };
                    }
                }
                else
                {
                    return new HttpResponseMessage()
                    {
                        Content = new StringContent("Email không hợp lệ")
                    };
                }
            }
            else
            {
                if (string.IsNullOrEmpty(email))
                {
                    return new HttpResponseMessage()
                    {
                        Content = new StringContent("Invalid email addrress")
                    };
                }
                if (PNLibraries.isValidEmail(email))
                {
                    var dbo = StaticData.emailRepo.GetOne(a => a.Email == email);
                    if (dbo != null)
                    {
                        return new HttpResponseMessage()
                        {
                            Content = new StringContent("Email has registered!")
                        };
                    }
                    else
                    {
                        TBEmail mail = new TBEmail();
                        mail.Email = email;
                        mail.Type = true;
                        mail.Enable = true;
                        StaticData.emailRepo.Save(null, mail);
                        return new HttpResponseMessage()
                        {
                            Content = new StringContent("Register successful!")
                        };
                    }
                }
                else
                {
                    return new HttpResponseMessage()
                    {
                        Content = new StringContent("Invalid email addrress")
                    };
                }
            }           
            
        }

        [HttpGet]
        public HttpResponseMessage CheckSupportStatus(string nick, string type)
        {
            string result = "";
            string yahooURL = "http://mail.opi.yahoo.com/online?u={nick}&m=a&t=0";
            string skypeURL = "http://mystatus.skype.com/{nick}.num";
            if (type == "1")//yahoo
            {
                WebClient client = new WebClient();
                string reply = client.DownloadString(yahooURL.Replace("{nick}", nick));
                if (reply.Substring(reply.Length - 10, 10) == "NOT ONLINE")
                {
                    result = "nok";
                }
                else
                {
                    result = "ok";
                }
            }
            else
            {
                WebClient client = new WebClient();
                string reply = client.DownloadString(skypeURL.Replace("{nick}", nick));
                if (reply == "2")
                {
                    result = "ok";
                }
                else
                {
                    result = "nok";
                }
            }
            return new HttpResponseMessage()
            {
                Content = new StringContent(result)
            };
        }

        [HttpGet]
        public HttpResponseMessage SendFeedback(string name, string nuoc, string tinh, string phone, string email, string add, string title, string message, string lang)
        {
            try
            {
                Feedback feed = new Feedback();
                feed.FeedbackID = Guid.NewGuid();
                feed.Name = name;
                feed.Title = title;
                feed.Email = email;
                feed.Phone = phone;
                feed.Message = message;
                feed.Country = nuoc;
                feed.Province = tinh;
                feed.Address = add;
                feed.IsRead = false;
                feed.Type = 0;
                feed.Lang = lang == "vn" ? true : false;
                feed.SentDate = DateTime.Now;
                StaticData.feedbackRepo.Save(null, feed);
                return new HttpResponseMessage()
                {
                    Content = new StringContent("ok")
                };
            }
            catch (Exception e)
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("nok")
                };
            }

        }

        [HttpGet]
        public HttpResponseMessage ValidateUsername(string username)
        {
            if(string.IsNullOrEmpty(username))
                return new HttpResponseMessage()
                {
                    Content = new StringContent("0")
                };
            if(username.Length<3)
                return new HttpResponseMessage()
                {
                    Content = new StringContent("0")
                };
            string flag = "ok";
            var user = StaticData.xuserRepo.GetOne(a => a.Username == username);
            if (user != null)
                flag = "0";
            return new HttpResponseMessage()
            {
                Content = new StringContent(flag)
            };
        }

        [HttpGet]
        public HttpResponseMessage ValidateEmail(string email)
        {
            if(string.IsNullOrEmpty(email))
                return new HttpResponseMessage()
                {
                    Content = new StringContent("0")
                };
            if(email.Length<3)
                return new HttpResponseMessage()
                {
                    Content = new StringContent("0")
                };
            string flag = "ok";
            var user = StaticData.xuserRepo.GetOne(a => a.Email == email);
            if (user != null)
                flag = "0";
            return new HttpResponseMessage()
            {
                Content = new StringContent(flag)
            };
        }

        [HttpGet]
        public HttpResponseMessage Register(string username, string email, string pass, string repass)
        {
            if(pass!=repass)
                return new HttpResponseMessage()
                {
                    Content = new StringContent("0")
                };
            MembershipCreateStatus createStatus;
            Membership.CreateUser(username, pass, email, null, null, true, null, out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                PNProject.EF.XUser xuser = new EF.XUser()
                {
                    UserID = Guid.NewGuid(),
                    Email = email,
                    Username = username,
                    Role = "Member",
                    IsBlocked = false
                };
                StaticData.xuserRepo.Save(null, xuser);
                Roles.AddUserToRole(username, "Member");
                return new HttpResponseMessage()
                {
                    Content = new StringContent("ok")
                };
            }
            else
                return new HttpResponseMessage()
                {
                    Content = new StringContent("0")
                };
        }

    }
}
